# -*- coding: utf-8 -*-

require 'kconv'
require 'iconv'

#$ic = Iconv.new('SJIS', 'UTF8')

def utf_sjis(str)
  
  ss = "―ソЫⅨ噂浬欺圭構蚕十申曾箪貼能表暴予禄兔喀媾彌拿杤歃濬畚秉綵臀藹觸軆鐔饅鷭偆砡".split(//u)
  tmp = str
  ss.each {|c|
    tmp = tmp.gsub(c, c + "\\")
    #print c + "\n"
  }
  
  tmp = tmp.gsub(/\n/,'')
  #tmp.chomp!
  #wk = "[#{NKF.guess(tmp).to_s}]"
  #tmp += wk
  
  begin
    wk = $ic.iconv(tmp)
  rescue
    wk = tmp.tosjis #+ "*"
  ensure
    wk = ""
  end
  
  #return tmp.tosjis
  #return $ic.iconv(tmp)
  return wk
  
end

def money_format(num)
  return (num.to_s =~ /[-+]?\d{4,}/) ? (num.to_s.reverse.gsub(/\G((?:\d+\.)?\d{3})(?=\d)/, '\1,').reverse) : num.to_s
end

def date_format_for_gengo(str)
  
  val = Date.strptime(str, "%Y-%m-%d")
  
  if val.year < 1989 then
    tmp = "S#{val.year - 1925}."
  else
    tmp = "H#{val.year - 1988}."
  end
  
  tmp += "#{val.month}.#{val.day}"
  
  return tmp
  
end

def debug_print(str)
  if $debug then
    print str
  end
end

def output_log(filename, text)
  f = File.open(filename,'a')
  f << text
  f.close()
end

